<template>
  <div class="ratesmanagement">
    <h3>添加费率表收费标准</h3>
    <hr />
    <el-form
      :inline="true"
      :model="ratesinfoList"
      ref="ratesinfoList"
      class="demo-form-inline"
      label-width="80px"
    >
      <el-form-item label="项目名" style="width: 30%">
        <el-input
          v-model="ratesinfoList.ratesPname"
          placeholder="请输入内容"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item
        label="最大人数"
        style="width: 30%"
        prop="ratesNumMax"
        :rules="[
          { required: true, message: '内容不能为空' },
          { type: 'number', message: '内容必须为数字值' },
        ]"
      >
        <el-input
          v-model.number="ratesinfoList.ratesNumMax"
          placeholder="请输入内容"
          autocomplete="off"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item
        label="最小人数"
        style="width: 30%"
        prop="ratesNumMin"
        :rules="[
          { required: true, message: '内容不能为空' },
          { type: 'number', message: '内容必须为数字值' },
        ]"
      >
        <el-input
          v-model.number="ratesinfoList.ratesNumMin"
          placeholder="请输入内容"
          autocomplete="off"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item
        label="单价"
        style="width: 30%"
        prop="ratesPrice"
        :rules="[
          { required: true, message: '内容不能为空' },
          { type: 'number', message: '内容必须为数字值' },
        ]"
      >
        <el-input
          v-model.number="ratesinfoList.ratesPrice"
          placeholder="请输入内容"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="是否启用" style="width: 35%">
        <el-select
          v-model="ratesinfoList.ratesStart"
          placeholder="请选择内容"
          style="width: 206.4px"
        >
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addFun">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="ratesListArr"
      border
      style="width: 100%; margin-top: 30px"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column type="index" label="序号" width="50"> </el-table-column>
      <el-table-column prop="ratesPname" label="项目名" width="300">
      </el-table-column>
      <el-table-column prop="ratesNumMax" label="最大人数" width="180">
      </el-table-column>
      <el-table-column prop="ratesNumMin" label="最小人数" width="180">
      </el-table-column>
      <el-table-column prop="ratesPrice" label="单价" width="180">
      </el-table-column>
      <el-table-column prop="ratesStart" label="是否启用" width="180">
        <template slot-scope="props">
          <el-switch
            v-model="props.row.ratesStart"
            active-value="1"
            inactive-value="0"
            active-text="是"
            inactive-text="否"
          >
          </el-switch>
        </template>
      </el-table-column>


      <el-table-column label="操作" width="187">
        <template slot-scope="scope">
          <el-button @click="updateFun(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="delFun(scope.row.ratesId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 模态框开始 -->
    <el-dialog
      title="修改费率表收费标准"
      :visible.sync="dialogFormVisible"
      width="62%"
    >
      <el-form
        :inline="true"
        :model="updateratesinfo"
        ref="updateratesinfo"
        class="demo-form-inline"
        label-width="80px"
      >
        <el-form-item label="项目名" style="width: 30%" size="small">
          <el-input
            v-model="updateratesinfo.ratesPname"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="最大人数"
          style="width: 30%"
          size="small"
          prop="ratesNumMax"
          :rules="[
            { required: true, message: '内容不能为空' },
            { type: 'number', message: '内容必须为数字值' },
          ]"
        >
          <el-input
            v-model.number="updateratesinfo.ratesNumMax"
            placeholder="请输入内容"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="最小人数"
          style="width: 30%"
          size="small"
          prop="ratesNumMin"
          :rules="[
            { required: true, message: '内容不能为空' },
            { type: 'number', message: '内容必须为数字值' },
          ]"
        >
          <el-input
            v-model.number="updateratesinfo.ratesNumMin"
            placeholder="请输入内容"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="单价"
          style="width: 30%"
          size="small"
          prop="ratesPrice"
          :rules="[
            { required: true, message: '内容不能为空' },
            { type: 'number', message: '内容必须为数字值' },
          ]"
        >
          <el-input
            v-model.number="updateratesinfo.ratesPrice"
            placeholder="请输入内容"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否启用" style="width: 35%" size="small">
          <el-select
            v-model="updateratesinfo.ratesStart"
            placeholder="请选择内容"
            style="width: 190.4px"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmFun">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totals"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  rateslist,
  addratesinfo,
  delratesinfo,
  updateratesinfo,
} from "@network/api/order.js";
export default {
  data() {
    return {
      ratesListArr: [],
      ratesinfoList: {
        beiyong1: "",
        beiyong2: 0,
        beiyong3: "",
        ratesId: "",
        ratesNumMax: "",
        ratesNumMin: "",
        ratesPname: "",
        ratesPrice: "",
        ratesStart: "",
      },
      dialogFormVisible: false,
      updateratesinfo: {},
      currentPage: 1,
      pageSize: 5,
      totals:0
    };
  },
  methods: {
    async rateslistFun() {
      let resrateslist = await rateslist(this.currentPage, this.pageSize);
      console.log("费率表数据", resrateslist);
      this.ratesListArr = resrateslist.data.data.ratesList;
      this.totals=resrateslist.data.data.total;
    },
    async addFun() {
      let resaddratesinfo = await addratesinfo(this.ratesinfoList);

      let { code, message } = resaddratesinfo.data;
      if (code && code == 200) {
        this.$message({
          message: message,
          type: "success",
        });
        this.rateslistFun();
      } else {
        this.$message({
          message: message,
          type: "error",
        });
      }
    },
    async delFun(ratesId) {
      let resdelratesinfo = await delratesinfo(ratesId);
      let { code, message } = resdelratesinfo.data;
      if (code && code == 200) {
        this.$message({
          message: message,
          type: "success",
        });
        this.rateslistFun();
      } else {
        this.$message({
          message: message,
          type: "error",
        });
      }
    },
    updateFun(obj) {
      this.dialogFormVisible = true;
      this.updateratesinfo = obj;
    },
    async confirmFun() {
      let resupdateratesinfo = await updateratesinfo(this.updateratesinfo);
      //   console.log("resupdateratesinfo", resupdateratesinfo);
      let { code, message } = resupdateratesinfo.data;
      if (code && code == 200) {
        this.$message({
          message: message,
          type: "success",
        });
        this.dialogFormVisible = false;
        this.rateslistFun();
      } else {
        this.$message({
          message: message,
          type: "error",
        });
      }
    },
    handleSizeChange(val) {
        this.currentPage = 1;
      this.pageSize = val;
      this.rateslistFun();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.rateslistFun();
    },
  },
  created() {
    this.rateslistFun();
  },
};
</script>

<style>
.ratesmanagement {
  background-color: #fff;
  padding: 40px 20px;
}
.demo-form-inline {
  margin-top: 30px;
}
.pagination{
    display: flex;
    justify-content: center;
    margin-top: 20px;
}
</style>